#coding: utf-8
#Created: 26.04.12 13:18

import urllib.request
import lxml.html
from Parsing import ParsingHelp, PositionEntity
import os

opener = ParsingHelp.auth_in_IE()

page = urllib.request.urlopen('http://huter.su/catalog/ustroystva-bespereboynogo-pitaniya/').read().decode('windows-1251')

document = lxml.html.document_fromstring(page)

items = document.xpath('//div[@class="item"]')

for item in items:
    name = item.xpath('a/text()')[0]

    entity = PositionEntity.HuterEntity(name)
    print(entity.nameForForm)
    link = 'http://huter.su' + item.xpath('a')[0].get('href')
    entityPage = urllib.request.urlopen(link).read().decode('windows-1251')

    documentEntity = lxml.html.document_fromstring(entityPage)

    trs = documentEntity.xpath('//table[@border="1"]//tr')
    #trs = documentEntity.xpath('//table[@style="border-collapse: collapse; "]//tr')

    tcTitles = []
    tcValues = []

    for tr in trs:
        tcTitles.append(tr.xpath('td')[0].xpath('text()')[0])
        tcValues.append(tr.xpath('td')[1].xpath('text()')[0])

    print(tcTitles)
    print(tcValues)
    entity.tcTitles = tcTitles
    entity.tcValues = tcValues

    if not os.path.isdir('Images\\' + entity.device):
        os.mkdir('Images\\' + entity.device)

    pictureLink = 'http://huter.su' + documentEntity.xpath('//img[@id="catalog_img"]')[0].get('src')
    picture = urllib.request.urlopen(pictureLink).read()
    file = open('Images\\' + entity.device  + '\\' + entity.name + pictureLink.strip()[-4:], 'wb')
    file.write(picture)
    file.close()

    ParsingHelp.add_to_IE(entity, opener)

    #print(entity.nameForForm)
    #print(entity.tags)
    #print(entity.descriptionForForm)